package com.learn.smart.framework.helper;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.learn.smart.framework.util.ReflectionUtil;

public class BeanHelper {
	
	private static final Map<Class<?>,Object> BEAN_MAP = new HashMap<Class<?>,Object>();
	
	static{
		Set<Class<?>> CLASS_SET=ClassHelper.getBeanClassSet();
		for(Class<?> cls : CLASS_SET){
			Object obj = ReflectionUtil.newInstance(cls);
			BEAN_MAP.put(cls, obj);
		}
	}
	
	public static void init(){
		System.out.println("通过class实例化所有bean");
	}
	
	public static Map<Class<?>,Object> getBeanMap(){
		return BEAN_MAP;
	}
	
	@SuppressWarnings("unchecked")
	public static <T> T getBean(Class<T> cls){
		if(!BEAN_MAP.containsKey(cls)){
			throw new RuntimeException("不存在bean："+cls);
		}
		return (T)BEAN_MAP.get(cls);
	}

}
